<?php
function gfci_custom_multi_register() {
	if ( !$options = get_option('custom_widget_gfci') )
		$options = array();
	$widget_ops = array('classname' => 'custom_widget_gfci', 'description' => __('Add a Google Friend Connect Custom Gadget'));
	$control_ops = array('width' => 420, 'height' => 420, 'id_base' => 'gfcicustom');
	$name = __('GFC Custom');

	$registered = false;
	foreach ( array_keys($options) as $o ) {
		// Old widgets can have null values for some reason
		if ( !isset($options[$o]['gadget_wid']) )
			continue;
		$id = "gfcicustom-$o"; // Never never never translate an id
		$registered = true;
		wp_register_sidebar_widget($id, $name, 'GFCIWidget_Custom', $widget_ops, array( 'number' => $o ));
		wp_register_widget_control($id, $name, 'widget_gfci_custom_control', $control_ops, array( 'number' => $o ));
	}
	
	// If there are none, we register the widget's existance with a generic template
	if ( !$registered ) {
            wp_register_sidebar_widget('gfcicustom-1', $name, 'GFCIWidget_Custom', $widget_ops, array( 'number' => -1 ) );
            wp_register_widget_control('gfcicustom-1', $name, 'widget_gfci_custom_control', $control_ops, array( 'number' => -1 ) );
        }
}

function GFCIWidget_Custom($args, $widget_args = 1) {
	extract( $args, EXTR_SKIP );
	if ( is_numeric($widget_args) )
		$widget_args = array( 'number' => $widget_args );
	$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
	extract( $widget_args, EXTR_SKIP );
	$canvas_path = get_option( gfci_canvas_path );
	$options = get_option('custom_widget_gfci');
	if ( !isset($options[$number]) )
		return;
        $gadget_wid = attribute_escape($options[$number]['gadget_wid']);
	$gadget_url = attribute_escape($options[$number]['gadget_url']);
	$gfci_border = attribute_escape($options[$number]['gfci_border']);
	$gfci_present = attribute_escape($options[$number]['gfci_present']);
	if ( "checked" == $gfci_present ) { $gfci_present2 = 'presentation:"canvas",'; }
	
  echo $before_widget;
  echo $before_title;
  echo $after_title;
?>
<div id="gfccustom-<?php echo $number; ?>" style="width:<?php echo $gadget_wid; ?>px;border:1px solid #<?php echo $gfci_border ?>;"></div>
<script type="text/javascript">
google.friendconnect.container.setParentUrl('<?php echo $canvas_path; ?>' /* location of rpc_relay.html and canvas.html */);
google.friendconnect.container.renderOpenSocialGadget(
 { id: 'gfccustom-<?php echo $number; ?>',<?php echo $gfci_present2; ?>
   url:'<?php echo $gadget_url; ?>',
   site: '<?php echo gfci_sit(); ?>'},
  skin);
</script>
<?php
  echo $after_widget;
}

function widget_gfci_custom_control($widget_args) {
	global $wp_registered_widgets;
	static $updated = false; // Whether or not we have already updated the data after a POST submit

	if ( is_numeric($widget_args) )
		$widget_args = array( 'number' => $widget_args );
	$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
	extract( $widget_args, EXTR_SKIP );

	// Data should be stored as array:  array( number => data for that instance of the widget, ... )
	$options_all = get_option('custom_widget_gfci');
	if ( !is_array($options_all) )
		$options_all = array();

	if ( !$updated && !empty($_POST['sidebar']) ) {
		// Tells us what sidebar to put the data in
		$sidebar = (string) $_POST['sidebar'];

		$sidebars_widgets = wp_get_sidebars_widgets();
		if ( isset($sidebars_widgets[$sidebar]) )
			$this_sidebar =& $sidebars_widgets[$sidebar];
		else
			$this_sidebar = array();

		foreach ( $this_sidebar as $_widget_id ) {
		if ( 'custom_widget_gfci' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
				$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
				if (!in_array("gfcicustom-$widget_number", $_POST['widget-id']))
				unset($options_all[$widget_number]);
			}
		}
                foreach ( (array) $_POST['gfc-custom-post'] as $widget_number => $posted ) {
			if (!isset($posted['gadget_wid']) && isset($options_all[$widget_number]))
                    continue;
                
                $options = array();
                $options['gadget_wid'] = $posted['gadget_wid'];
		$options['gadget_url'] = $posted['gadget_url'];
		$options['gfci_present'] = $posted['gfci_present'];
		$options['gfci_border'] = $posted['gfci_border'];
		$options_all[$widget_number] = $options;
            }
            update_option('custom_widget_gfci', $options_all);
            $updated = true;
	    
}
	if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
		$gadget_wid = '282';
		$number = '%i%';
		$gadget_url = 'http://os.ilike.com/gadget/playlist';
		$gfci_border = 'cccccc';
		$gfci_present = '';

	} else {
		$gadget_wid = $options_all[$number]['gadget_wid'];
		$gadget_url = $options_all[$number]['gadget_url'];
		$gfci_present = $options_all[$number]['gfci_present'];
		$gfci_border = $options_all[$number]['gfci_border'];
	}

?>
<h3>Custom Gadget Width and Source</h3>
<table>
<tr>
<td><label for="<?php echo $number; ?>-gadget-wid">
  Width:
  </label></td>
<td><input class="widefat" 
    type="text"
    name="gfc-custom-post[<?php echo $number; ?>][gadget_wid]" 
    id="<?php echo $number; ?>-gadget-wid" 
    value="<?php echo $gadget_wid; ?>" /></td>
</tr>
<tr>
<td><label for="<?php echo $number; ?>-gadget-hei">
Gadget URL:
  </label></td>
<td colspan="2"><input class="widefat" type="text" 
    name="gfc-custom-post[<?php echo $number; ?>][gadget_url]" 
    id="<?php echo $number; ?>-gadget-url" 
    value="<?php echo $gadget_url; ?>"
    size="40" /></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-gfci-border">
Border:</label></td>
<td><input class="widefat"
    name="gfc-custom-post[<?php echo $number; ?>][gfci_border]"
    type="text"
    id="<?php echo $number; ?>-gfci-border"
    value="<?php echo $gfci_border; ?>"
    size="7"/></td>
    <td><input type="hidden" id="gfc-custom-submit-<?php echo $number; ?>" name="gfc-custom-submit-<?php echo $number; ?>" value="1" /></td>
</tr>
<tr><td><label for="<?php echo $number; ?>-gfci-border">
Canvas View?:
<td><input class="widefat"
    name="gfc-custom-post[<?php echo $number; ?>][gfci_present]"
    type="checkbox"
    id="<?php echo $number; ?>-gfci-present"
    value="checked" <?php if ( "checked" == $gfci_present ){ echo 'checked="checked"'; } ?> />
    </td>
    <td><input type="hidden" id="gfc-custom-submit-<?php echo $number; ?>" name="gfc-custom-submit-<?php echo $number; ?>" value="1" /></td>
</tr>
</table>
<?php
}
?>
